package com.huaxin.hxcloud.fragment;

import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import android.widget.TextView;

import com.emmp_ard.generateqr.QRcodegenerate;
import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Global;
import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxin.hxcloud.R;

import org.json.JSONObject;

import butterknife.BindView;

/**
 * Created by hebing on 2017/4/7.
 */

public class GenCodeFragment extends BaseFragment {

    @BindView(R.id.toolbar)
    Toolbar  toolbar;
    @BindView(R.id.gen_code_nice_name)
    TextView niceName;
    @BindView(R.id.gen_code_company)
    TextView companyTxt;
    private QRcodegenerate qRcodegenerate;
    @BindView(R.id.gen_code_iv)
    ImageView       qrImage;
    @BindView(R.id.gen_code_icon)
    AvatarImageView iconImage;

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_gen_code;
    }

    @Override
    protected void onResumeView() {
        super.onResumeView();
        ((BaseActivity) getActivity()).setSupportActionBar(toolbar);
        toolbar.setTitle("我的二维码");
    }

    @Override
    public void onResume() {
        super.onResume();
        qRcodegenerate = new QRcodegenerate(getContext());
        if (Global.user != null) {
            loadText(niceName, Global.user.getNickName());
            if (Global.user.getOrgCode() == null) {
                loadText(companyTxt, "游客身份");
            } else {
                loadText(companyTxt, Global.user.getCompany());
            }
            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(getContext(), Global.user.getImgUrl(), Global.user.getNickName(), iconImage);

            setQrImage();
        }
    }

    @Override
    protected void initData() {
        super.initData();
    }

    /**
     * 生成二维码
     */
    private void setQrImage() {
       try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("HuaXinData", "HuaXinData");
            jsonObject.put("nameID", Global.user.getUserId()+"");
            jsonObject.put("phone", Global.user.getTelPhone());
            jsonObject.put("typeValue", "fre");
            jsonObject.put("name", Global.user.getNickName());
            jsonObject.put("imgeUrl", Global.user.getImgUrl());
            qRcodegenerate.generate(qrImage, jsonObject.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }

      /*  qRcodegenerate.generate(qrImage, "HuaXinData" + "#&" + "fre" + "#&" +
                Global.user.getUserId() + "#&" + Global.user.getNickName() + "#&"
                + Global.user.getImgUrl() + "#&" + Global.user.getTelPhone());*/

    }
}
